package mp

import (
	"duoke/internal/dao"
	"duoke/model"
	"duoke/service"
	"duoke/tables"
	"duoke/utils/gconv"
	"duoke/utils/redislib"
	"duoke/utils/request"
	"duoke/utils/response"
	"duoke/utils/valid"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"time"
)

var CourseOrder = new(courseOrderCtl)

type courseOrderCtl struct{}

func (ctl *courseOrderCtl) Save(c *gin.Context) {
	var req model.CourseOrderReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, valid.GetError(err.(validator.ValidationErrors), req), nil)
		return
	}
	user, err := service.User.Info(c, "openid,id,acid,ip_addr")
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	redisKey := fmt.Sprintf("course_order_%d_%d_%d", req.VideoId, user.Id, req.BuyType)
	redisGet := redislib.Redis.Get(c, redisKey)
	OrderNO := ""
	if redisGet.Val() != "" {
		OrderNO = redisGet.Val()
	} else {
		req.UserId = user.Id
		req.Openid = user.Openid
		req.Acid = user.Acid
		req.IpAddr = user.IpAddr
		OrderNO, err = service.CourseOrder.Save(c, &req)
		if err != nil {
			response.Error(c, err.Error(), nil)
			return
		}
		redislib.Redis.Set(c, redisKey, OrderNO, 1*time.Minute)
	}
	response.Success(c, "success", gin.H{
		"order_no": OrderNO,
	})
}
func (ctl *courseOrderCtl) PayOrder(c *gin.Context) {
	req := request.JsonMap(c)
	orderNo := gconv.String(req["order_no"])
	var m tables.CourseOrder
	err := dao.CourseOrder.Ctx(c).Where("order_no=?", orderNo).Scan(&m)
	redisKey := fmt.Sprintf("course_order_%d_%d_1", m.VideoId, m.UserId)
	if err != nil {
		redislib.Redis.Del(c, redisKey)
		response.Error(c, err.Error(), nil)
		return
	}
	if m.Id == 0 {
		redislib.Redis.Del(c, redisKey)
		response.Error(c, "该订单不存在", nil)
		return
	}
	if orderNo == "" {
		redislib.Redis.Del(c, redisKey)
		response.Error(c, "订单号不能为空", nil)
		return
	}
	order, err := service.CourseOrder.PayOrder(c, &m)
	if err != nil {
		redislib.Redis.Del(c, redisKey)
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "success", order)
}

// PointsPurchase 积分购买
func (ctl *courseOrderCtl) PointsPurchase(c *gin.Context) {
	req := request.JsonMap(c)
	orderNo := gconv.String(req["order_no"])
	var m tables.CourseOrder
	err := dao.CourseOrder.Ctx(c).Where("order_no=?", orderNo).Scan(&m)
	redisKey := fmt.Sprintf("course_order_%d_%d_2", m.VideoId, m.UserId)
	if m.State == 1 {
		redislib.Redis.Del(c, redisKey)
		response.Error(c, err.Error(), nil)
		return
	}
	if err != nil {
		redislib.Redis.Del(c, redisKey)
		response.Error(c, err.Error(), nil)
		return
	}
	if m.Id == 0 {
		redislib.Redis.Del(c, redisKey)
		response.Error(c, "该订单不存在", nil)
		return
	}
	if orderNo == "" {
		redislib.Redis.Del(c, redisKey)
		response.Error(c, "订单号不能为空", nil)
		return
	}
	err = service.CourseOrder.PointsPurchase(c, &m)
	if err != nil {
		redislib.Redis.Del(c, redisKey)
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "success", nil)
}

// CarminePayment 卡密购买
func (ctl *courseOrderCtl) CarminePayment(c *gin.Context) {
	req := request.JsonMap(c)
	orderNo := gconv.String(req["order_no"])
	var m tables.CourseOrder
	err := dao.CourseOrder.Ctx(c).Where("order_no=?", orderNo).Scan(&m)
	redisKey := fmt.Sprintf("course_order_%d_%d_2", m.VideoId, m.UserId)
	defer redislib.Redis.Del(c, redisKey)
	if m.State == 1 {
		response.Error(c, err.Error(), nil)
		return
	}
	if err != nil || m.Id == 0 {
		response.Error(c, "该订单不存在", nil)
		return
	}
	if orderNo == "" {
		response.Error(c, "订单号不能为空", nil)
		return
	}
	carmineId := gconv.Int(req["carmine_id"])
	if carmineId == 0 {
		response.Error(c, "订单号不能为空", nil)
		return
	}
	err = service.CourseOrder.CarminePayment(c, &m, carmineId)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "success", nil)
}

// BuyInfo 订单详情
func (ctl *courseOrderCtl) BuyInfo(c *gin.Context) {
	service.CourseOrder.TimerOrder()
}
